<?php
header("Content-Type: text/html; charset=iso-8859-1");
session_start();
@$ftmp = $_FILES['image']['tmp_name'];
@$oname = $_FILES['image']['name'];
@$fsize = $_FILES['image']['size'];
@$ftype = $_FILES['image']['type'];
$id = (!empty($_SESSION['id'])) ? $_SESSION['id'] : "";
$cat = (!empty($_SESSION['cat'])) ? $_SESSION['cat'] : "";
$div = (!empty($_REQUEST['div'])) ? $_REQUEST['div'] : "info";
$types = array(
	1=>"gif",
	2=>"jpeg",
	3=>"png",
	4=>"swf",
	5=>"psd",
	6=>"bmp"
	);
if(IsSet($ftmp)) :
	if(!$id){
		if(!isset($_SESSION['rand'])){
			$rand = rand();
			$_SESSION['rand'] = $rand;		
		} 
		else $rand = $_SESSION['rand'];
		$nb = 0;
		if(!is_dir("images/$cat/tmp_$rand")) mkdir("images/$cat/tmp_$rand",0777);	
	}
 	$list_files = array();
    $dir = ($id) ? "images/$cat/$id" : "images/$cat/tmp_$rand";
    if ($handle_folder = opendir($dir)) {
       while (false !== ($file = readdir($handle_folder))) {
           if($file != "." && $file != ".." && !strstr($file,"__MACOSX")){
				$list_files[] = $dir."/".$file;
				if(ereg("img([0-9]*)",$file,$regs)){
					if($nb < $regs[1]) $nb = $regs[1];
				}
			} 
       }
       closedir($handle_folder);
    }
	$nb++;
	$type = $types[exif_imagetype($ftmp)];
	@$fname = "img".$nb.".".($types[exif_imagetype($ftmp)])."_tmp";
	$newname = "img".$nb.".".($types[exif_imagetype($ftmp)]);
	if(move_uploaded_file($ftmp,"$dir/".$fname)){
		$filename = $fname;
		list($width, $height) = getimagesize("$dir/".$filename);
		if($width < 500){
			rename("$dir/".$fname,"$dir/".$newname);
		}
		else{
			$new_width = 500;
			$new_height = $height * 500 / $width;
			$image_p = imagecreatetruecolor($new_width, $new_height);

			if($type == "jpeg"){
				$image = imagecreatefromjpeg("$dir/".$filename);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
				imagejpeg($image_p,"$dir/".$newname, 100);
				unlink("$dir/".$fname);
			}
			elseif($type == "gif"){
				$image = imagecreatefromgif("$dir/".$filename);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
				imagegif($image_p,"$dir/".$newname, 100);
				unlink("$dir/".$fname);				
			}
			elseif($type == "png"){
				$image = imagecreatefrompng("$dir/".$filename);
				imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
				imagepng($image_p,"$dir/".$newname, 100);
				unlink("$dir/".$fname);				
			}
		}
		$fname = $newname;
		chmod($dir."/".$newname,0644);
	}
	$tag = ($id) ? $id : "tmp_$rand";

	echo "<html><head>\n";
	echo "<script src='javascripts/vbcll.js' type='text/javascript' charset='utf-8'></script>	\n";
	echo "<script>\n";
	echo "function insertcode(tag,textarea)\n";
	echo "{\n";
	echo "	var zorglub\n";
	echo "	//IE support\n";
	echo "	if (window.parent.document.selection) {\n";
	echo "		textarea.focus();\n";
	echo "		sel = window.parent.document.selection.createRange();\n";
	echo "		contenu=sel.text\n";
    echo "\n";
	echo "		if(contenu == '' || contenu == null){\n";
	echo "			zorglub=1\n";
	echo "		}\n";
    echo "\n";
	echo "		if(zorglub!=1)\n";
	echo "		{			\n";
	echo "			sel.text = contenu+tag;\n";
	echo "		}\n";
	echo "		else\n";
	echo "		{\n";
	echo "			textarea.value+=contenu+tag;\n";
	echo "		}\n";
    echo "\n";
	echo "	}\n";
	echo "	//MOZILLA/NETSCAPE support\n";
	echo "	else if (textarea.selectionStart || textarea.selectionStart == '0') {\n";
	echo "		var startPos = textarea.selectionStart;\n";
	echo "		var endPos = textarea.selectionEnd;\n";
	echo "		var contenu = textarea.value.substring(startPos, endPos)\n";
    echo "\n";
	echo "		if(contenu == '' || contenu == null){\n";
	echo "			zorglub=1\n";
	echo "		}\n";
    echo "\n";
	echo "		if(zorglub!=1 || startPos!=0)\n";
	echo "		{			\n";
	echo "			textarea.value = textarea.value.substring(0, startPos) + contenu + tag + textarea.value.substring(endPos, textarea.value.length);\n";
	echo "		}\n";
	echo "		else\n";
	echo "		{\n";
	echo "			textarea.value+=contenu+tag;\n";
	echo "		}		\n";
	echo "	}\n";
	echo "}\n";
	echo "function preview(texte,div_id){\n";
	echo "	if(texte){\n";
	echo "		texte = file('prepare_info.php?texte='+escape(texte));\n";
	echo "		par.getElementById(div_id).innerHTML = texte;\n";
	echo "	}\n";
	echo "}\n";
	echo "\n";
	echo "var par = window.parent.document;\n";
	echo "var list = par.getElementById('list');\n";
    echo "\n";
	echo "list.innerHTML = 'Code &agrave; ins&eacute;rer : ==img:$tag:$fname==';\n";
	echo "insertcode('\\n\\n==img:$tag:$fname==\\n\\n',par.myform.corps);\n";
	echo "preview(par.myform.corps.value,'corps_$div');\n";
	echo "</script>\n";
	echo "</head>\n";
	echo "</html>\n";
    exit();
endif;
?>
<html><head>
<script language="javascript">
function upload(){
// hide old iframe
    var par = window.parent.document;
    var num = par.getElementsByTagName('iframe').length - 1;
    var iframe = par.getElementsByTagName('iframe')[num];
    iframe.className = 'hidden';

    // create new iframe
    var new_iframe = par.createElement('iframe');
    new_iframe.src = 'img_upload.php';
    new_iframe.frameBorder = '0';
    par.getElementById('iframe').appendChild(new_iframe);

    // add image progress
    var list = par.getElementById('list');
    var new_div = par.createElement('div');
    var new_img = par.createElement('img');
    new_img.src = 'images/icon_indicator.gif';
    new_img.className = 'load';
    new_div.appendChild(new_img);
    list.appendChild(new_div);

    // send
    var imgnum = list.getElementsByTagName('div').length - 1;
    document.iform.imgnum.value = imgnum;
    document.iform.submit();
}
</script>
<style>
body {vertical-align:top;}
</style>
<link rel='stylesheet' href='styles/vbcll.css' type = 'text/css' />
</head>
<body>
<div id = 'corps_img'>
<p class = 'center bold'>Ajouter une image</p>
<form name="iform" action="" method="post" enctype="multipart/form-data">
<p class = 'center'><input id="file" type="file" name="image" size = '10' onchange="upload()" />
<input type="hidden" name="imgnum" /></p>
</form>
</div>
</body>
</html>
